<?
if (!defined('MODULE_FILE')){
	die ("cant access file directly!");
}

$mid 	= ( ( true == isset( $_GET['mid'] ) && true == valStr( $_GET['mid'] ) ) ? $_GET['mid'] : NULL );
$smid 	= ( ( true == isset( $_GET['smid'] ) && true == valStr( $_GET['smid'] ) ) ? $_GET['smid'] : NULL );

autoLoader();

$objBaseCharges 	 = new BaseCharges();
$objCharges 	     = new Charges();
$objChargeCatagories = new ChargeCatagories();
$objChargeTypes      = new ChargeTypes();
$objDepartments      = new Departments();
$objPatients         = new Patients();
?>
<html>
<head>
<?php require_once 'commonJS.php'; ?>
</head>
<body>
	<div style="margin-left:20px; margin-top: 10px;">
		<p>
			<a class="btn btn-outline btn-primary btn-xs" id="active" href="modules.php?file=charges&mid=<? echo $mid;?>&smid=<? echo $smid;?>" >Create New Charge</a>&nbsp;
			<a class="btn btn-outline btn-primary btn-xs" id="active" href="modules.php?file=view_charges&type=view&mid=<? echo $mid;?>&smid=<? echo $smid;?>" >View All Charges</a>
		</p>
	</div>
<div class="col-lg-8">
<div class="panel panel-primary">
<div class="panel-heading"><i class="fa fa-bell fa-fw"></i> Charges</div>
<div class="panel-body">
	<form METHOD="POST" action="modules.php?file=insert_charges&type=insert&mid=<? echo $mid; ?>&smid=<? echo $smid; ?>" id="formSubmit" name="formSubmit" >
		<?php
		$intChargeId         = NULL;
		$intChargeTypeId     = NULL;
	    $intChargeCatagoryId = NULL;
	    $intMinAmount        = NULL;
	    $intMaxAmount        = NULL;
	    $intRate             = NULL;
        $intDepartmentId     = NULL;
	    $intPatientId        = NULL;
		
		
	
				
		if( true == isset( $_GET['charge_id'] ) && true == valStr( $_GET['charge_id'] ) ) {
			$intChargeId = $_GET['charge_id'];
			
			$objBaseCharges = $objCharges->fetchChargesById( $_GET['charge_id'] );
			
			if( true == valObj( $objBaseCharges, 'BaseCharges' ) ) {
				$intChargeTypeId 	 = $objBaseCharges->getChargeTypeId();
				$intChargeCatagoryId = $objBaseCharges->getChargeCatagoryId();
				$intMinAmount 		 = $objBaseCharges->getMinAmount();
				$intMaxAmount 		 = $objBaseCharges->getMaxAmount();
				$intRate 		     = $objBaseCharges->getRate();
				$intDepartmentId 	 = $objBaseCharges->getDepartmentId();
				$intPatientId 		 = $objBaseCharges->getPatientId();
			}
		} else if( true == valObj( $objBaseChargeCatagories, 'BaseCharges' ) ) {
			$intChargeId = $objBaseChargeCatagories->fetchNextId();
		}
		?> 
	
		<table width="80%" cellspacing="0" cellpadding="3">

			<tr><td height="10" colspan="2"><input id="charge_id" Name="charge_id" type="hidden"  style="width:100px;"  value="<?php echo $intChargeId;?>"></td></tr>
          	<tr valign="top">
				<td  width="220">Charge Type</td>
				<td>
		        <select class="validate[required]" name="charge_type_id" id="charge_type_id" style="width:202px;">
				<option value=''>-Select-</option>
				<?php
					$arrChargeTypes = $objChargeTypes->fetchAllChargeTypes();
					if( true == valStr( $arrChargeTypes ) ) {
						foreach( $arrChargeTypes as $objChargeType ) {
							echo '<option' . ( ( $intChargeTypeId == $objChargeType->getId() ) ? ' selected' : '' ) . ' value=' . $objChargeType->getId() . '>' . $objChargeType->getName() . '</option>';
						}
					}
					?>
				</select>
				</td>
			</tr>
			<tr>
				<td colspan="2" height="10px"></td>
			</tr>
			 	<tr valign="top">
				<td  width="220">Charge Catagory</td>
				<td>
		        <select class="validate[required]" name="charge_catagory_id" id="charge_catagory_id" style="width:202px;">
				<option value=''>-Select-</option>
				<?php
					$arrChargeCatagories = $objChargeCatagories->fetchAllChargeCatagories();
					if( true == valStr( $arrChargeCatagories ) ) {
						foreach( $arrChargeCatagories as $objChargeCat ) {
							echo '<option' . ( ( $intChargeCatagoryId == $objChargeCat->getId() ) ? ' selected' : '' ) . ' value=' . $objChargeCat->getId() . '>' . $objChargeCat->getName() . '</option>';
						}
					}
					?>
				</select>
				</td>
			</tr>
			<tr>
				<td colspan="2" height="10px"></td>
			</tr>
		    <tr valign="top">
				<td  width="220">Minimum Amount</td>
				<td><input id="min_amount" Name="min_amount" type="text" class="validate[required]" value="<?php echo $intMinAmount; ?>" style="width:202px;"></td>
			</tr>
			<tr>
				<td colspan="2" height="10px"></td>
			</tr>
			  <tr valign="top">
				<td  width="220">Maximum Amount</td>
				<td><input id="max_amount" Name="max_amount" type="text" class="validate[required]" value="<?php echo $intMaxAmount; ?>" style="width:202px;"></td>
			</tr>
			<tr>
				<td colspan="2" height="10px"></td>
			</tr>
			<tr valign="top">
				<td  width="220">Rate</td>
				<td><input id="rate" Name="rate" type="text" class="validate[required]" value="<?php echo $intRate; ?>" style="width:202px;"></td>
			</tr>
			<tr>
				<td colspan="2" height="10px"></td>
			</tr>
			<tr valign="top">
				<td  width="220">Department</td>
				<td>
		        <select class="validate[required]" name="department_id" id="department_id" style="width:202px;">
				<option value=''>-Select-</option>
				<?php
					$arrobjDepartments = $objDepartments->fetchAllDepartments();
					
					if( true == valStr( $arrobjDepartments ) ) {
						foreach( $arrobjDepartments as $Department ) {
							echo '<option' . ( ( $intDepartmentId == $Department->getId() ) ? ' selected' : '' ) . ' value=' . $Department->getId() . '>' . $Department->getName() . '</option>';
						}
					}
					?>
				</select>
				</td>
			</tr>
			<tr>
				<td colspan="2" height="10px"></td>
			</tr>
			
			<tr valign="top">
				<td  width="220">Patient Name</td>
				<td>
		        <select class="validate[required]" name="patient_id" id="patient_id" style="width:202px;">
				<option value=''>-Select-</option>
				<?php
					$arrobjPatients = $objPatients->fetchAllPatients();
					
					if( true == valStr( $arrobjPatients ) ) {
						foreach( $arrobjPatients as $objPatient ) {
							echo '<option' . ( ( $intPatientId == $objPatient->getId() ) ? ' selected' : '' ) . ' value=' . $objPatient->getId() . '>' . $objPatient->getFirstName() . '&nbsp;'. $objPatient->getLastName(). '</option>';
						}
					}
					?>
				</select>
				</td>
			</tr>
			
			<tr>
				<td colspan="2" height="10px"></td>
			</tr>
			<tr valign="top">
				<td class="mandatory" width="220">&nbsp;</td>
				<td>
				<?php
						if( true == isset( $_GET['charge_id'] ) && true == valStr( $_GET['charge_id'] ) ) {
							echo "<input type=hidden name=operation value=update>";
							echo "<input type=submit value=UPDATE id=Submit name=Submit>";
						} else {
							echo "<input type=hidden name=operation value=insert>";
							echo "<input type=submit value=SUBMIT id=Submit name=Submit>";
						}
					?>
				
				</td> 				
			</tr>
		</table>
</div>
</div>
</div>	
</form>
</body>
</html>